﻿using ETC.DLEDLightingSystemPlus.Core;
using ETC.DLEDLightingSystemPlus.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;

namespace ETC.DLEDLightingSystemPlus.Models
{
    public class TestStation : Bandit.UI.MVVM.NotifyObject, IDisposable
    {
        /// <summary>
        /// 工位信息
        /// </summary>
        public StationInfo StationInfo { get; set; }

        /// <summary>
        /// 测试信息
        /// </summary>
        public TestInfo TestInfo { get; set; }

        /// <summary>
        /// 配置信息
        /// </summary>
        public ConfigInfo ConfigInfo { get; set; }


        /// <summary>
        /// 测试版
        /// </summary>
        public DLEDLightingHelper DLEDLightingHelper { get; set; }

        #region 任务栏状态
        /// <summary>
        /// Status
        /// </summary>
        public Status Status { get; set; }
        #endregion

        #region 其它信息状态
        /// <summary>
        /// Other Status
        /// </summary>
        private bool _SeeStatus = true;
        public bool SeeStatus { get; set; }
        #endregion

        #region 测试板状态
        /// <summary>
        /// BambooPlus Test Board Status
        /// </summary>
        public Status SerialStatus { get; set; }
        #endregion

        private bool _IsStop = false;
        public bool IsStop 
        { 
            get { return _IsStop; }
            set { 
                _IsStop = value;
                OnPropertyChanged(nameof(IsStop));
            } 
        }

        //private bool _IsPowerOFF = false;
        //public bool IsPowerOFF
        //{
        //    get { return _IsPowerOFF; }
        //    set
        //    {
        //        _IsPowerOFF = value;
        //        OnPropertyChanged(nameof(IsPowerOFF));
        //    }
        //}

        #region LED视图
        private int _DiyLEDRows;
        public int DiyLEDRows
        {
            get { return _DiyLEDRows; }
            set
            {
                _DiyLEDRows = value;
                OnPropertyChanged(nameof(DiyLEDRows));
            }
        }

        private int _DiyLEDCols;
        public int DiyLEDCols
        {
            get { return _DiyLEDCols; }
            set
            {
                _DiyLEDCols = value;
                OnPropertyChanged(nameof(DiyLEDCols));
            }
        }

        private List<DIYLedModel> _LedModelList = new List<DIYLedModel>();

        public List<DIYLedModel> LedModelList
        {
            get { return _LedModelList; }
            set
            {
                _LedModelList = value;
                OnPropertyChanged(nameof(LedModelList));
            }
        }
        #endregion

        private Dictionary<string, string> _PortNames;
        /// <summary>
        /// PortNames
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public Dictionary<string, string> PortNames
        {
            get { return _PortNames; }
            set
            {
                _PortNames = value;
                OnPropertyChanged("PortNames");
            }
        }

        private double IoutCurrentValue = 0;

        public void LoadPortNames()
        {
            var ports = new Dictionary<string, string>();

            string[] portNames = Bandit.Helpers.BanditSerialPort.GetPortNames();

            if (portNames != null && portNames.Length > 0)
            {

                foreach (var item in portNames)
                {
                    ports.Add(item.GetBetweenValue("(", ")"), item);
                }

            }
            ports.Add("Com999", "Com999");

            this.PortNames = ports;
        }


        public TestStation(StationInfo stationInfo)
        {
            this.TestInfo = new TestInfo("Name");
            this.StationInfo = stationInfo;

            this.LoadConfigInfo();

            this.LoadLEDInterface();

            LoadPortNames();

            this.Status = new Status("Ready.", MyBrushes.White);
            this.SerialStatus = new Status(string.Format("[COM] is not connected!"));

        }
        public void LoadConfigInfo()
        {
            this.ConfigInfo = new ConfigInfo();

            if (!string.IsNullOrWhiteSpace(this.StationInfo.TestConfigFile))
            {
                
                string fileName = this.StationInfo.TestConfigFile;

                Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute);

                if (!uri.IsAbsoluteUri)
                {
                    fileName = this.GetBaseDirectory() + "Configs\\" + this.StationInfo.TestConfigFile;
                }

                if (File.Exists(fileName))
                {
                    this.ConfigInfo.TestConfig = TestConfig.LoadConfig(fileName, this.StationInfo.Number, true);

                    //ComputeIoutCurrentValue();

                    this.InitTestItemResult();

                }
            }
            else
            {
                //this.ConfigInfo.TestConfig = new TestConfig();

                //this.ConfigInfo.TestConfig.InitTestConfig();
            }
        }

        public void InitTestItemResult()
        {
            this.TestInfo.StationName = this.ConfigInfo.TestConfig.Station;

            this.TestInfo.TestItemResults.Clear();

            foreach (TestItem testItem in this.ConfigInfo.TestConfig.TestItems)
            {

                this.TestInfo.TestItemResults.Add(

                   new TestItemResult(testItem.Num, testItem.TestItemCode, testItem.Name, testItem.IsTestVisibility, testItem.IsTest,testItem.IsSetCurrentValue,testItem.SetCurrentValue, testItem.IsSetMcValue, testItem.SetMcValue,testItem.IsSetDcValue,testItem.SetDcValue,testItem.IsSetBcValue,testItem.SetBcValue,testItem.IsSetScaleValue,testItem.SetScaleValue,testItem.IsSetILedValue,testItem.SetILedValue,testItem.RealityCurrentValue, testItem.DelayTime, testItem.Limit, testItem.Unit,testItem.IsReaValue,testItem.IsSetCompensationValue,testItem.SetCompensationValue)

                );
            }

        }

        #region 初始化LED视图
        public void LoadLEDInterface()
        {
            if (ConfigInfo.TestConfig != null)
            {
                this.LedModelList = new List<DIYLedModel>();

                this.DiyLEDRows = ConfigInfo.TestConfig.DiyLEDRows;

                this.DiyLEDCols = ConfigInfo.TestConfig.DiyLEDCols;

                foreach (DIYLedModel dIYLedModel in ConfigInfo.TestConfig.DiyLedModelList)
                {

                    this.LedModelList.Add(new DIYLedModel()
                    {
                        Name = dIYLedModel.Name,
                        Position = dIYLedModel.Position,
                        ICID = dIYLedModel.ICID,
                        LEDID = dIYLedModel.LEDID,
                        LedType = "Idle",
                        IsFlag = dIYLedModel.IsFlag,
                        //Remark = ""
                    }); ;
                }
            }
        }
        #endregion

        public void Dispose()
        {
            this.StationInfo = null;
            this.TestInfo = null;
            this.ConfigInfo = null;
            this.LedModelList = null;

            App.DLEDLightingHelpers.Remove(this.DLEDLightingHelper);
            this.DLEDLightingHelper.Close();
            this.DLEDLightingHelper = null;
            
            GC.SuppressFinalize(this);
        }


    }
}
